﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MyShop.HttpHandler
{
    /// <summary>
    /// Summary description for MainHandler
    /// </summary>
    public class MainHandler : IHttpHandler
    {
        public class ProductObj
        {
            public string ID { get; set; }
            public string GroupID { get; set; }
            public string CompanyID { get; set; }
            public string ModelName { get; set; }
            public string Description { get; set; }
            public string Price { get; set; }
            public string Count { get; set; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get { return false; }
        }

        public Dictionary<string, object> GetProductConfiguration()
        {
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString =
                System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString;
            sqlcon.Open();
            SqlCommand com = new SqlCommand("select * from Product", sqlcon);
            //DataTable dataTable = new DataTable();
            //SqlDataAdapter dataAdapter = new SqlDataAdapter();
            //try
            //{
            //    dataAdapter.SelectCommand = com;
            //    dataAdapter.Fill(dataTable);
            //}
            //catch (Exception)
            //{

            //    throw;
            //}

            SqlDataReader reader = com.ExecuteReader();
            List<ProductObj> mainResult = new List<ProductObj>();
            while (reader.Read())
            {

                mainResult.Add(new ProductObj
                    {
                        ID = reader["ID"].ToString(),
                        GroupID = reader["GroupID"].ToString(),
                        CompanyID = reader["CompanyID"].ToString(),
                        ModelName = reader["ModelName"].ToString(),
                        Description = reader["Description"].ToString(),
                        Price = reader["Price"].ToString(),
                        Count = reader["Count"].ToString(),
                    });
            }

            sqlcon.Close();
            Dictionary<string, object> objects = new Dictionary<string, object>();
            objects.Add("ListProduct", mainResult);
            return objects;
        }
    }
}